/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */


package edu.uc.labs.MobilePrinting.OSUtils;

import static edu.uc.labs.MobilePrinting.Configurator.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;

import edu.uc.labs.MobilePrinting.Prefs.Preferences;

public class StartupManager implements StartupCommand {
	
	private String script = null;
	private String command = null;
	
	
	public StartupManager(){
		LOGGER.debug("OS = " + OS);
		if (OS.startsWith("Mac")) {
			script = SCRIPTS + DELIM + "mac" + STARTUP_SCRIPT
					+ ".sh";
			command = "sh";
		} else if (OS.equalsIgnoreCase("Windows XP")) {
			script = SCRIPTS + DELIM + "win" + STARTUP_SCRIPT
					+ "xp.bat";
			command = "start";
		}else if (OS.equalsIgnoreCase("Windows 7") || OS.equalsIgnoreCase("Windows Vista")){
			script = SCRIPTS + DELIM + "win" + STARTUP_SCRIPT + "7.bat";
			command = "start";
		}
		
		LOGGER.debug("The command to run is: " + command + " " + script);
	}
	
	public void execute( Preferences pref ){
		if(OS.startsWith("Linux")){
			return;
		}
		// Is the app already in the startup list?
		LOGGER.debug("Checking if the app is already in the startup list...");
		boolean isInstalled = isInstalled();
		
		if (pref.isStartAtLogin() && !isInstalled) {
			// The user wants it installed in the login items
			// and it currently isn't
			String output = exec("add", START_PATH);
			LOGGER.debug("The output of the add command is: " + output);
		} else if (!pref.isStartAtLogin() && isInstalled) {
			// The user wants to remove it from the login items
			exec("remove", APPNAME);
		}
	}
	
	public String exec(String action, String arg) {
		
		String output = null;
		
		CommandLine commandLine = null;
		
		try {
			ByteArrayOutputStream stdout = new ByteArrayOutputStream();
			PumpStreamHandler psh = new PumpStreamHandler(stdout);
			commandLine = CommandLine.parse(command);
			commandLine.addArgument(script);
			commandLine.addArgument(action);
			commandLine.addArgument(arg);
			DefaultExecutor executor = new DefaultExecutor();
			executor.setStreamHandler(psh);
			@SuppressWarnings("unused")
			int r = executor.execute(commandLine);
			output = stdout.toString();
		} catch (ExecuteException e){
			LOGGER.error( e.toString() );
			LOGGER.error("The command = " + commandLine.toString());
			return "True";
		} catch (IOException e) {
			LOGGER.error("Could not " + action + " for " + arg + ": ");
			LOGGER.error(e.toString());
			return "false";
		}	
		return output;
	}

	@Override
	public boolean isInstalled() {
		String output = null;
		boolean installed = false;
		output = exec("check", APPNAME);
		if(output == null || !output.equalsIgnoreCase("True"));
		else {
			installed = true;
		}
		return installed;
		
	}

	@Override
	public boolean install() {
		String output = exec("add", START_PATH);
		boolean successful = false;
		
		if (output.equalsIgnoreCase("True")){
			successful = true;
		}
		return successful;
	}
	

	@Override
	public boolean remove() {
		String output = exec("remove", APPNAME);
		boolean successful = false;
		if(output.equalsIgnoreCase("True")){
			successful = true;
		}
		return successful;
	}
	

}
